package org.example.controller;


import org.example.common.R;
import org.example.interceptor.UserHolder;
import org.example.pojo.entity.ApUser;
import org.example.pojo.vo.PersonVo;
import org.example.service.IApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static org.example.utils.AliOssUtil.upload;

/**
 * <p>
 * APP用户信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-26
 */
@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/front")
public class ApUserController {

    @Autowired
    private IApUserService apUserService;

    /**
     * 登录
     *
     * @param apUser
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody ApUser apUser) {
        R loginVo = apUserService.login(apUser);
        return loginVo;
    }

    /**
     * 注册
     *
     * @param apUser
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody ApUser apUser) {
        R registerInfo = apUserService.register(apUser);
        return registerInfo;
    }

    /**
     * 获取个人信息
     *
     * @return
     */
    @GetMapping("/person/info")
    public R info() {
        PersonVo personVo = apUserService.info();
        return R.ok(personVo);
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public R uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String imgUrl = upload(file.getOriginalFilename(), file.getInputStream());
        return R.ok(imgUrl);
    }


    /**
     * 文件上传
     *
     * @param imgUrl
     * @return
     * @throws IOException
     */
    @GetMapping("/edit/img")
    public R uploadFile(@RequestParam String imgUrl) {
        ApUser apUser = new ApUser();
        apUser.setImage(imgUrl);
        apUser.setId(UserHolder.getUserId());
        apUserService.updateById(apUser);
        return R.ok("修改成功");
    }

}
